import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

const user = defineStore('user', {
  state: () => ({
    userInfo: localStorage.getItem('user:userInfo') ?
      JSON.parse(localStorage.getItem('user:userInfo')) : [],
    ws: null,
    list: {},
    userList: []
  }),
  actions: {
    setUserInfo(data) {
      console.log(data);
      this.userInfo = data
      localStorage.setItem('user:userInfo', JSON.stringify(data))
    },
    setWs(data) {
      this.ws = data
    },
    addList(data) {
      let sessionId
      if (data.toId === this.userInfo.id || data.fromId === this.userInfo.id) {
        sessionId = this.userInfo.id === data.toId ? data.fromId : data.toId
      }
      this.list[sessionId] = this.list[sessionId] || []
      this.list[sessionId].push(data)
    },
    setUserList(data) {
      this.userList = data
    }
  }
})
export default user